package cn.sh.ideal.web.configuration.management.whiteListConfig.controller;


import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import cn.sh.ideal.web.configuration.management.whiteListConfig.entity.WhiteListDto;
import cn.sh.ideal.web.configuration.management.whiteListConfig.entity.WhiteListQueryCondition;


@Controller
@PropertySource(value = {"classpath:jdbc.properties"})
@RequestMapping(value = "/data")
public class whiteListConfigDataController {
	@Bean
	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}
	
	private RestTemplate rs = new RestTemplate();
	
	@Value("${server_address}")
	private String serverAddress;
	
	@RequestMapping(value = "/getWhiteListConfig", method = RequestMethod.POST)
	@ResponseBody
	public String getWarningConfig(@RequestBody WhiteListQueryCondition parameter){
		String url = serverAddress+"/getWhiteListConfig?hostId=" + parameter.getHostId()
				+"&rulesId=" + parameter.getRules()
				+"&page=" + parameter.getPage()
				+"&pageSize=" + parameter.getSize();
		
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url,
				HttpMethod.GET, entity, String.class);

		String result = exchange.getBody();
		
		return result;
	}
	
	@RequestMapping(value = "/getWhiteListHostId", method = RequestMethod.POST)
	@ResponseBody
	public String getWarningMonitoringIndex(){
			String url = serverAddress+"/getWhiteListHostId";
	
			HttpHeaders headers = new HttpHeaders();
			MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
			headers.setContentType(type);
			HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
			
			ResponseEntity<String> exchange = rs.exchange(url,
					HttpMethod.GET, entity, String.class);
			
			String result = exchange.getBody();
			
			return result;
	}
	
	@RequestMapping(value = "/getWhiteListRules", method = RequestMethod.POST)
	@ResponseBody
	public String getWarningMonitoringKpi(){
			String url = serverAddress+"/getWhiteListRules";
	
			HttpHeaders headers = new HttpHeaders();
			MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
			headers.setContentType(type);
			HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
			
			ResponseEntity<String> exchange = rs.exchange(url,
					HttpMethod.GET, entity, String.class);
			
			String result = exchange.getBody();
			
			return result;
	}
	
	@RequestMapping(value = "/insertWhiteList", method = RequestMethod.POST)
	@ResponseBody
	public String insertWhiteList(@RequestBody WhiteListDto parameter){
		
		String url = serverAddress+"/insertWhiteList?host=" + parameter.getHost()
				+"&rules=" + parameter.getRules();
		
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url,
				HttpMethod.GET, entity, String.class);
		
		//String result = exchange.getBody();
		
		return "";
	}

	/**
	 * 白名单配置页面删除配置controller
	 * @param: 
	 * @author: zhangyy
	 * @Date:   2018.05.16
	 */
	@RequestMapping(value = "/delectWhiteConfig")
	@ResponseBody
	public String delectWhiteConfig(@RequestBody String parameter){
		ArrayList<String> parameterList = (ArrayList<String>) JSON.parseObject(parameter, new TypeReference<ArrayList<String>>(){});
		for(String param : parameterList){
			String url = serverAddress+"/delectWhiteList?id=" + param;
			HttpHeaders headers = new HttpHeaders();
			MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
	        headers.setContentType(type);
			HttpEntity<String> httpentity = new HttpEntity<>(new String(), headers);

			ResponseEntity<String> exchange = rs.exchange(url,
					HttpMethod.GET, httpentity, String.class);
		}
		
		return "";
	}
	
	
}
